package com.sjh.application

import android.app.Application
import java.lang.Exception

/**
 *
 * @author Extends
 * @date 2020/6/21
 */
class ApplicationUtils private constructor(val application: Application) {
    companion object{
        private lateinit var applicationUtils: ApplicationUtils

        fun init(context:Application){
            applicationUtils =
                ApplicationUtils(context)
            applicationUtils.init()
        }
    }

    private fun init(){
        val result = ClassUtils.getFileNameByPackageName(
            application,
            "com.sjh.application.inject"
        )

        //获取所有的Application
        val applicationArray = arrayListOf<String>()
        result.forEach {
            try {
                val obj = Class.forName(it).newInstance()
                if (obj is ApplicationInjectInterface){
                    obj.init(applicationArray)
                }
            }catch (e:Exception){
                e.printStackTrace()
            }
        }
        //初始化所有的Application
        applicationArray.forEach {
            try {
                val obj = Class.forName(it).newInstance()
                if (obj is ApplicationInterface){
                    obj.init(application)
                }
            }catch (e:Exception){
                e.printStackTrace()
            }
        }

    }

}